package com.lokiy.kit

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import com.lokiy.annotation.FragmentInfo
import com.lokiy.core.activity.CommonActivity
import com.lokiy.core.adapter.ViewBindingAdapter
import com.lokiy.di.FragmentInfoItem
import com.lokiy.kit.databinding.ActivityMainItemBinding
import com.lokiy.kit.ext.applicationCxt
import kotlin.reflect.KClass
import kotlin.reflect.full.findAnnotation

class MainActivity : AppCompatActivity() {

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        applicationCxt = application
        setContentView(R.layout.activity_main)
        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)

        toolbar.setNavigationOnClickListener {
            finish()
        }
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.addItemDecoration(DividerItemDecoration(this, LinearLayoutCompat.VERTICAL))
        val adapter = ViewBindingAdapter<KClass<*>, ActivityMainItemBinding> { holder, kClass ->
            val text = kClass.findAnnotation<FragmentInfo>()?.value ?: kClass.simpleName
            holder.binding.text.text = "${text}(${kClass.simpleName})"
            holder.itemView.setOnClickListener {
                @Suppress("UNCHECKED_CAST")
                CommonActivity.startActivity(this@MainActivity, kClass.java as Class<out Fragment?>, null)
            }
        }
        recyclerView.adapter = adapter
        adapter.refresh(FragmentInfoItem.items)
    }
}